package gr.handmademarket.ui.forms;

import gr.handmademarket.bl.operations.LoginOperation;
import gr.handmademarket.po.interfaces.User;
import gr.handmademarket.ui.WebHandMadeSession;
import gr.handmademarket.ui.pages.HomePage;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;

public class LoginForm 
extends Form<User>{

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 1L;

	private TextField<String> usernameField = new RequiredTextField<String>("username");
	
	private TextField<String> passwordField = new PasswordTextField("password");
	
	private Button signInBtn = new Button("loginBtn"){
		/**
		 * serialVersionUID
		 */
		private static final long serialVersionUID = 1L;

		public void onSubmit() {
			LoginOperation op = new LoginOperation();
			op.setUsername(usernameField.getModelObject());
			op.setPassword(passwordField.getModelObject());
			op.execute();
			User user = op.getUser();
			if (user != null){
				WebHandMadeSession session = (WebHandMadeSession) WebHandMadeSession.get();
				session.setUser(user);
			}
			setResponsePage(HomePage.class);
		};
	};
	
	public LoginForm(String id, IModel<User> model) {
		super(id, model);
		add(usernameField,passwordField,signInBtn);
	}

}
